/**
 *
 * @author Ryan Thomas  <s548169@sbuniv.edu>
 * @Version 1.0_FINAL
 */
package MultiUserChatServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerInputMonitorThread extends Thread{
    
    private Controller controlNode;
    private ThreadLinkedList threads;
    private DebugHandler debug;
    
    public ServerInputMonitorThread(Controller controlNode, ThreadLinkedList threads, DebugHandler debug){
        this.controlNode = controlNode;
        this.threads = threads;
        this.debug = debug;
    }
    
    @Override
    public void run(){
        while(controlNode.get()){
            try {
                BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
                String inputText = inFromUser.readLine();
                switch (inputText){
                        case "GETTHREADS":
                            long[] threadIDs = threads.getThreadIDs();
                            System.out.println("Active Threads: ");
                            for(int i = 0; i<threadIDs.length; i++){
                                System.out.println("ThreadID: " + threadIDs[i]);
                            }
                            break;
                        case "EXITEXIT":
                            controlNode.disable();
                            threads.killAllSockets();
                            Socket termSocket = new Socket("127.0.0.1", 65530);
                            termSocket.close();
                            break;
                        case "INITENCRYPTION":
                            System.out.println("Encryption is not available in this version");
                            break;
                        case "DEBUG":
                            debug.enable();
                            System.out.println("DebugMode Enabled");
                            break;
                        default:
                            System.out.println("Invalid Command");
                }
            } catch (IOException ex) {
                    if(debug.get()){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }         
}
